home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 October / CHIP Turkiye Ekim 2000.iso / prog / naps / 04 / setup.exe / Gnucleus / ViewSearchSpy.cpp < prev    next >
C/C++ Source or Header  |  2000-07-15  |  7KB  |  275 lines

  1. /********************************************************************************
  2.  
  3.     Gnucleus - A node application for the Gnutella network
  4.     Copyright (C) 2000 John Marshall
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19.     For support, questions, comments, etc...
  20.     E-Mail: 
  21.         Steve Kemp <skx@tardis.ed.ac.uk>
  22.     
  23.     Address:
  24.         Private ;) 
  25.  
  26. ********************************************************************************/
  27.  
  28. // ViewSearchSpy.cpp : implementation file
  29. //
  30.  
  31. #include "stdafx.h"
  32. #include "Gnucleus.h"
  33. #include "MainFrm.h"
  34.  
  35. #include "GnucleusDoc.h"
  36. #include "ViewTransfer.h"
  37. #include "ViewSearchSpy.h"
  38.  
  39. #include "IPFilter.h"
  40.  
  41. #include "GnuTransfer.h"
  42. #include "GnuHash.h"
  43. #include "GnuSock.h"
  44. #include "GnuControl.h"
  45.  
  46.  
  47. #ifdef _DEBUG
  48. #define new DEBUG_NEW
  49. #undef THIS_FILE
  50. static char THIS_FILE[] = __FILE__;
  51. #endif
  52.  
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CViewSearchSpy
  55.  
  56. IMPLEMENT_DYNCREATE(CViewSearchSpy, CFormView)
  57.  
  58. CViewSearchSpy::CViewSearchSpy()
  59.     : CFormView(CViewSearchSpy::IDD)
  60. {
  61.     //{{AFX_DATA_INIT(CViewSearchSpy)
  62.     //}}AFX_DATA_INIT
  63.  
  64.     m_paused = false;
  65. }
  66.  
  67. CViewSearchSpy::~CViewSearchSpy()
  68. {
  69. }
  70.  
  71. void CViewSearchSpy::DoDataExchange(CDataExchange* pDX)
  72. {
  73.     CFormView::DoDataExchange(pDX);
  74.     //{{AFX_DATA_MAP(CViewSearchSpy)
  75.     DDX_Control(pDX, IDC_LIST_SEARCH_RESULTS, m_lstResults);
  76.     //}}AFX_DATA_MAP
  77. }
  78.  
  79.  
  80. BEGIN_MESSAGE_MAP(CViewSearchSpy, CFormView)
  81.     //{{AFX_MSG_MAP(CViewSearchSpy)
  82.     ON_WM_SIZE()
  83.     ON_BN_CLICKED(IDC_PAUSE_RESUME, OnPauseResume)
  84.     ON_BN_CLICKED(IDC_COPY_SEARCH, OnCopySearch)
  85.     ON_NOTIFY(NM_DBLCLK, IDC_LIST_SEARCH_RESULTS, OnDblclkListSearchResults)
  86.     //}}AFX_MSG_MAP
  87. END_MESSAGE_MAP()
  88.  
  89. /////////////////////////////////////////////////////////////////////////////
  90. // CViewSearchSpy diagnostics
  91.  
  92. #ifdef _DEBUG
  93. void CViewSearchSpy::AssertValid() const
  94. {
  95.     CFormView::AssertValid();
  96. }
  97.  
  98. void CViewSearchSpy::Dump(CDumpContext& dc) const
  99. {
  100.     CFormView::Dump(dc);
  101. }
  102. #endif //_DEBUG
  103.  
  104. /////////////////////////////////////////////////////////////////////////////
  105. // CViewSearchSpy message handlers
  106.  
  107. void CViewSearchSpy::OnInitialUpdate() 
  108. // Initializes all the dialog controls on the local view
  109. {
  110.     CFormView::OnInitialUpdate();
  111.     
  112.     Doc = (CGnucleusDoc *) GetDocument();
  113.     Comm = Doc->GnuComm;
  114.  
  115.     CRect rect;
  116.     m_lstResults.GetWindowRect(&rect);
  117.  
  118.     m_lstResults.InsertColumn( 0, "Terms" );
  119.     m_lstResults.SetColumnWidth(-1, LVSCW_AUTOSIZE_USEHEADER);
  120.  
  121.     m_lstResults.SetExtendedStyle(LVS_EX_FULLROWSELECT);
  122.  
  123.     GetParentFrame()->GetClientRect(&rect);
  124.     OnSize(SIZE_RESTORED, rect.right - 4, rect.bottom - 4);
  125.  
  126. }
  127.  
  128.  
  129. void CViewSearchSpy::OnSize(UINT nType, int cx, int cy) 
  130. // Adjusts dialog controls when window in resized
  131. {
  132.     // Make sure dialog controls exist
  133.     if(m_lstResults.m_hWnd != NULL)
  134.     {
  135.         CRect rect;
  136.         GetClientRect( &rect );
  137.         
  138.         // We only have one column, so make it full width.
  139.         // (Do this before we modify the rect.)
  140.         m_lstResults.SetColumnWidth( 0, rect.Width() - 6 );
  141.  
  142.         CRect pauseBtn;
  143.         ::GetClientRect(GetDlgItem( IDC_PAUSE_RESUME )->GetSafeHwnd(), &pauseBtn );
  144.         CRect copyBtn;
  145.         ::GetClientRect(GetDlgItem( IDC_COPY_SEARCH )->GetSafeHwnd(), ©Btn );
  146.  
  147.         // List fills the whole window, except for the bottom row, which is
  148.         // as big as the height of the pause button
  149.         rect.bottom -= pauseBtn.Height();
  150.  
  151.         m_lstResults.MoveWindow( rect );
  152.         
  153.         rect.top = rect.bottom;
  154.         rect.bottom += pauseBtn.Height();
  155.         rect.right  = pauseBtn.Width();
  156.         GetDlgItem( IDC_PAUSE_RESUME )->MoveWindow( rect );
  157.  
  158.         int height = 0;
  159.         int width  = 0;
  160.         height  = copyBtn.Height();
  161.         width   = copyBtn.Width();
  162.  
  163.         copyBtn.top    = rect.top;
  164.         copyBtn.bottom = copyBtn.top + height;
  165.         copyBtn.left   = rect.right + 4;
  166.         copyBtn.right  = copyBtn.left + width;
  167.  
  168.         GetDlgItem( IDC_COPY_SEARCH )->MoveWindow( copyBtn );
  169.  
  170.     }
  171.  
  172.     CFormView::OnSize(nType, cx, cy);
  173. }
  174.  
  175.  
  176. /**
  177.  * Add the search request to the list window, unless we're
  178.  * paused.
  179.  */
  180. void CViewSearchSpy::AddSearchTerm( CString searchText )
  181. {
  182.     int item_count = 0;
  183.     if ( !m_paused )
  184.     {
  185.         while((item_count = m_lstResults.GetItemCount()) + 1 > m_lstResults.GetCountPerPage() && item_count )
  186.         {
  187.             m_lstResults.DeleteItem(item_count - 1);
  188.         }
  189.         m_lstResults.InsertItem( 0, searchText );
  190.     }
  191. }
  192.  
  193. /**
  194.  * Toggle the updating of the list.
  195.  */
  196. void CViewSearchSpy::OnPauseResume()
  197. {
  198.     if ( m_paused )
  199.     {
  200.         GetDlgItem( IDC_PAUSE_RESUME )->SetWindowText( "Pause" );
  201.         m_paused = false;
  202.     }
  203.     else
  204.     {
  205.         GetDlgItem( IDC_PAUSE_RESUME )->SetWindowText( "Resume" );
  206.         m_paused = true;
  207.     }
  208. }
  209.  
  210.  
  211. /**
  212.   * Start a search that is the same as that which is currently
  213.   * selected.
  214.   */
  215. void CViewSearchSpy::OnCopySearch()
  216. {
  217.     CString TestCase, Keyword, Speed;
  218.  
  219.     CComboBox *cbKeyword = (CComboBox *) ( (CMainFrame *) AfxGetMainWnd() )->GetDialogBar()->GetDlgItem(IDC_COMBO_SEARCH);
  220.  
  221.     int nSelected = m_lstResults.GetNextItem( -1, LVNI_ALL | LVNI_SELECTED); 
  222.     if ( nSelected == -1 ) 
  223.         return;
  224.  
  225.     Keyword = m_lstResults.GetItemText( nSelected, 0);
  226.  
  227.     CString Search = "Searching for \"";
  228.             Search += Keyword;
  229.             Search += "\"";
  230.  
  231.  
  232.     // Make sure search isnt null
  233.     if(Keyword == "")
  234.         return;
  235.  
  236.     if(TestCase != Keyword)
  237.         cbKeyword->InsertString(0, Keyword);
  238.  
  239.     // Bring window to front if it already exists
  240.     bool Found = 0;
  241.     CString Title;
  242.     POSITION pos = ((CGnucleusApp *) AfxGetApp())->MasterDoc->GetFirstViewPosition();
  243.  
  244.     while (pos != NULL)
  245.     {
  246.         CView* pView = ((CGnucleusApp *) AfxGetApp())->MasterDoc->GetNextView(pos);
  247.         pView->GetParentFrame()->GetWindowText(Title);
  248.  
  249.         if(Title == Search)
  250.         {
  251.             pView->GetParentFrame()->BringWindowToTop();
  252.             
  253.             return;
  254.         }
  255.     }
  256.     
  257.     // Create new search window
  258.     CFrameWnd* pNewFrame = ((CGnucleusApp *) AfxGetApp())->m_pSearchViewTemplate->CreateNewFrame(((CGnucleusApp *) AfxGetApp())->MasterDoc, NULL);
  259.  
  260.     pNewFrame->ModifyStyle(FWS_ADDTOTITLE, 0);
  261.     pNewFrame->SetWindowText(Search);
  262.  
  263.     ((CGnucleusApp *) AfxGetApp())->m_pSearchViewTemplate->InitialUpdateFrame(pNewFrame, ((CGnucleusApp *) AfxGetApp())->MasterDoc);
  264.  
  265. }
  266.  
  267. //
  268. //  A double-click on a search term is synonymous with a
  269. // copy search request.
  270. void CViewSearchSpy::OnDblclkListSearchResults(NMHDR* pNMHDR, LRESULT* pResult) 
  271. {
  272.     OnCopySearch();
  273.     *pResult = 0;
  274. }
  275.